import React from "react";
import { Form, Upload, Button, Icon, message } from "antd";

const UploadForm = Form.create({ name: "form_in_upLoad" })(
  class extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        fileList: [],
      };
    }

    validateUpload = (rule, value, callback) => {
      if (value && value.file.name.split(".")[value.file.name.split(".").length - 1] !== "lrc") {
        callback("请上传正确的.lrc格式的授权文件！");
      } else {
        callback();
      }
    };
		
		handleUpload = (file) => {
		  if(file.name.split(".")[file.name.split(".").length - 1] !== "lrc") {
		    return false;
		  }else{
		    return true;
		  }
		}

    handleOnChange = info => {
      let fileList = [...info.fileList];
      fileList = fileList.slice(-1);
      this.setState({ fileList });
      if (info.file.status === "done") {
        // this.setState({
        //   userName: info.file.response.data.user,
        //   beforeInfo: new Date(
        //     info.file.response.data.start
        //   ).toLocaleDateString(),
        //   endInfo: new Date(info.file.response.data.expire).toLocaleDateString(),
        // });
        message.success(`${info.file.name} 文件上传成功`);
        window.location.reload();
      } else if (info.file.status === "error") {
        message.error(`${info.file.name} 文件上传失败.`);
      }
    };

    render() {
      const { getFieldDecorator } = this.props.form;
      const { fileList } = this.state;
      return (
        <Form>
          <Form.Item>
            {getFieldDecorator("upLoad", {
              rules: [
                {
                  validator: this.validateUpload,
                },
              ],
            })(
              <Upload
                name="license"
                action={`${window.host}/license/upload`}
                onChange={this.handleOnChange}
                beforeUpload={this.handleUpload}
                fileList={fileList}
              >
                <Button>
                  <Icon type="upload" /> 上传授权文件
                </Button>
              </Upload>
            )}
          </Form.Item>
        </Form>
      );
    }
  }
);

export default UploadForm;
